<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Exceptions\ErrorCode;
use App\Models\BadWord;
use App\Models\UpgradeInfo;
use App\Requests\FeatureRequest;
use Illuminate\Support\Facades\Cache;
use Singiu\WordBan\WordBan;

class FeatureController extends Controller
{
    public function changeVersion(FeatureRequest $request)
    {
        $data = $request->validated();
        $data['user_id'] = auth()->user()->id;
        if (!UpgradeInfo::where('platform', $data['platform'])->update($data)) {
            UpgradeInfo::create($data);
        }
        return response_success();
    }

    public function checkText(FeatureRequest $request)
    {
        $text = $request->get('text');
        // 加载敏感词库。
        $bad_words = Cache::remember('bad_words', 10, function () {
            return BadWord::pluck('name')->all();
        });
        WordBan::load($bad_words);
        if (WordBan::scan($text)) {
            throw new ApiException('字符串中含有不合适的字符！', ErrorCode::BAD_WORD);
        }
        return response_success();
    }
}